<template>
  <dtDialog
    :title="modalTitle"
    :visible.sync="dialogVisible"
    :lock-scroll="false"
    :append-to-body="true"
    :is-loading="loading"
    @closeDialog="onModalClosed"
    @comfirmBtn="onSubmit()"
  >
    <el-form
      ref="form"
      slot="content"
      label-width="140px"
      :model="form"
      :rules="rules"
    >
      <el-row>
        <el-col :span="24" v-if="action == 'edit'">
          <el-form-item label="监控编码" prop="monitorCode">
            <el-input v-model.trim="form.monitorCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="监控名称" prop="monitorName">
            <el-input v-model.trim="form.monitorName" placeholder="请输入监控名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="上游系统" prop="systemId">
            <el-select v-model.trim="form.systemId" placeholder="请选择上游系统">
              <el-option
                v-for="item in systemList"
                :key="item.systemId"
                :label="item.systemName"
                :value="item.systemId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="监控接入地址" prop="monitorAddr">
            <el-input v-model.trim="form.monitorAddr" placeholder="请输入监控接入地址" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="接入方式" prop="accessType">
            <el-select v-model.trim="form.accessType" placeholder="请选择接入方式">
              <el-option label="摄像头" value="1" />
              <el-option label="视频流" value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.accessType != 2">
          <el-form-item label="设备类型" prop="equipmentId">
            <el-select v-model.trim="form.equipmentId" placeholder="请选择设备类型" filterable>
              <el-option
                v-for="item in equipmentList"
                :key="item.id"
                :label="item.equipmentName + '-' + item.equipmentModel"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否自动录制监控" prop="isAutomatic">
            <el-radio-group v-model="form.isAutomatic">
              <el-radio label="1">是</el-radio>
              <el-radio label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </dtDialog>
</template>

<script>
import { editVideoMonitor, addVideoMonitor } from '@/api/videoFusion/monitorAccess'

export default {
  props: {
    selectList: {
      type: Array,
      default: () => []
    },
    equipmentList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        monitorCode: '',
        monitorName: '',
        systemId: '',
        monitorAddr: '',
        accessType: '',
        equipmentId: '',
        isAutomatic: '2'
      },
      rules: {
        monitorCode: [{ required: true, message: '请输入监控编码', trigger: ['blur'] }],
        monitorName: [{ required: true, message: '请输入监控名称', trigger: ['blur'] }],
        systemId: [{ required: true, message: '请选择上游系统', trigger: ['change'] }],
        monitorAddr: [{ required: true, message: '请输入监控接入地址', trigger: ['blur'] }],
        accessType: [{ required: true, message: '请选择接入方式', trigger: ['change'] }],
        equipmentId: [{ required: true, message: '请选择设备类型', trigger: ['change'] }],
        isAutomatic: [{ required: true, message: '请选择是否自动录制监控', trigger: ['change'] }]
      },
      modalTitle: '',
      loading: false,
      dialogVisible: false,
      action: ''
    }
  },
  computed: {
    systemList() {
      return this.selectList.slice(1)
    }
  },
  methods: {
    onModalClosed() {
      this.initModel()
    },
    onSubmit() {
      this.$refs.form.validate((result, message) => {
        if (result) {
          this.loading = true
          if (this.form.accessType == 2) {
            this.form.equipmentId = ''
          }
          if (this.action == 'edit') {
            editVideoMonitor(this.form).then((res) => {
              this.$dtModal.msgSuccess(res.message)
              this.$emit('success')
              this.loading = false
              this.dialogVisible = false
            }).catch(() => {
              this.loading = false
            })
          } else if (this.action == 'add') {
            addVideoMonitor(this.form).then((res) => {
              this.$dtModal.msgSuccess(res.message)
              this.$emit('success')
              this.loading = false
              this.dialogVisible = false
            }).catch(() => {
              this.loading = false
            })
          }
        }
      })
    },
    add() {
      this.dialogVisible = true
      this.modalTitle = '新增监控'
      this.action = 'add'
    },
    edit(row) {
      this.dialogVisible = true
      this.modalTitle = '修改监控'
      this.action = 'edit'
      this.form = { ...row }
    },
    initModel() {
      this.$refs.form.resetFields()
      this.form = {
        monitorCode: '',
        monitorName: '',
        systemId: '',
        monitorAddr: '',
        accessType: '',
        equipmentId: '',
        isAutomatic: '2'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
